<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/14
 * Time: 17:04
 */

namespace common\business\company;


use common\business\CommonHelper;
use common\business\task\TaskHandler;
use common\business\task\TaskHelper;
use common\business\task\TaskResponseData;
use common\consts\CommonResources;
use common\consts\Consts;
use common\models\Task;
use common\result\ActionResult;
use yii\base\Exception;
use yii\base\UserException;

class CompanyTaskHandler extends TaskHandler
{
//    /**
//     * @var int
//     */
//    protected $taskUpdateTimes = 0;

    /**
     * @param Task|null|array $task
     * @return TaskResponseData
     */
    public function process($task){
        $requestData = json_decode($task->request_data, true);
//        if (!isset($requestData['department_id'])) {
//            throw new UserException('参数输入有误' . $task->request_data);
//        }

        $writeBackHandler = function ($totalCount, $finishCount, $meessage) use ($task){
            TaskHelper::writeBackFinishPercentage($task, $totalCount, $finishCount, $meessage);
        };

//        $this->taskUpdateTimes = 0;
        $service = new CompanyService();
//        $result = $service->syncAllFromHr();
        $result = $service->sync($writeBackHandler);

        if (!$result->isSuccess){
            return new TaskResponseData(TaskResponseData::CODE_ERROR, CommonResources::ACTION_FAIL_MSG('Sync', 'Company', $result->message));
        }
        return new TaskResponseData(TaskResponseData::CODE_SUCCESS, CommonResources::ACTION_SUCCESS_MSG_DETAIL('Sync', 'Company', $result->message));
    }

    /**
     * @param Task|null|array $task
     * @return bool
     */
    public function isNeedNotifyException($task)
    {
        return true;
    }
}